package com.shine.commonlibrary.utils.updateapp;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;

import com.shine.commonlibrary.utils.utils.LogUtils;


/**
 * Created by M_genius on 2017/9/1 15:57.
 * 描述：更新app的工具
 */
public class UpdateAppUtils {

    private final String TAG = "UpdateAppUtils";


    private Activity activity;

    //服务器apk版本
    private int serverVersionCode = 0;
    private String serverVersionName = "";

    //apk存储路径
    private String apkPath = "";

    //当前apk的版本
    private int localVersionCode = 0;
    private String localVersionName = "";

    //下载是否显示通知
    public static boolean showNotification = true;

    private String updateInfo = "";


    private UpdateAppUtils(Activity activity) {
        this.activity = activity;
        getAPPLocalVersion(activity);
    }

    public static UpdateAppUtils from(Activity activity) {
        return new UpdateAppUtils(activity);
    }


    public UpdateAppUtils apkPath(String apkPath) {
        this.apkPath = apkPath;
        return this;
    }


    public UpdateAppUtils showNotification(boolean showNotification) {
        this.showNotification = showNotification;
        return this;
    }

    public UpdateAppUtils updateInfo(String updateInfo) {
        this.updateInfo = updateInfo;
        return this;
    }


    public UpdateAppUtils serverVersionCode(int serverVersionCode) {
        this.serverVersionCode = serverVersionCode;
        return this;
    }

    public UpdateAppUtils serverVersionName(String serverVersionName) {
        this.serverVersionName = serverVersionName;
        return this;
    }

    //获取apk的版本号 currentVersionCode
    private void getAPPLocalVersion(Context ctx) {
        PackageManager manager = ctx.getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(ctx.getPackageName(), 0);
            localVersionName = info.versionName; // 版本名
            localVersionCode = info.versionCode; // 版本号
            LogUtils.d("versionName:" + localVersionName);
            LogUtils.d("versionCode:" + localVersionCode);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void update() {

//        if (serverVersionCode > localVersionCode && serverVersionName.equals(localVersionName)) {
            realUpdate();
//        }
    }

    private void realUpdate() {
        ConfirmDialog dialog = new ConfirmDialog(activity, new UpdateAppCallback() {
            @Override
            public void callback(int position) {
                if (position == 1) {
                    if (isWifiConnected(activity)) {
                        DownloadAppUtils.downloadForAutoInstall(activity, apkPath, "demo.apk", serverVersionName);
                    } else {
                        new ConfirmDialog(activity, new UpdateAppCallback() {
                            @Override
                            public void callback(int position) {
                                if (position == 1) {
                                    DownloadAppUtils.downloadForAutoInstall(activity, apkPath, "demo.apk", serverVersionName);
                                }
                            }
                        }).setContent("目前手机不是WiFi状态\n确认是否继续下载更新？").show();
                    }
                }
            }
        });

        String content = "发现新版本:" + serverVersionName + "\n是否下载更新?";
        if (!TextUtils.isEmpty(updateInfo)) {
            content = "发现新版本:" + serverVersionName + "是否下载更新?\n\n" + updateInfo;
        }
        dialog.setContent(content);
        dialog.setCancelable(false);
        dialog.show();
    }


    /**
     * 检测wifi是否连接
     */
    public static boolean isWifiConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            NetworkInfo networkInfo = cm.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                return true;
            }
        }
        return false;
    }

}
